//TODO: License Header.

/*
This code is licensed under the BSD license.

*/
package analysis.metrics

import primitives.cluster.ClusterHead
import primitives.graph.Node
import search.util.TreeLoader2
import static Math.min
import primitives.cluster.*;
import primitives.graph.*;

class TopLevelCyclomaticMetric extends Metric{

	def static outEdges(IClusterLevel c){
        return edgesFilteredBy(c,{
                trans -> 
                !c.nodes.contains(trans.destinationNode)
          
        });
    }

	def static intraEdges(IClusterLevel c){
		return edgesFilteredBy(c,{
                trans -> 
                c.nodes.contains(trans.destinationNode)
          
        });
	}

	def static edgesFilteredBy(IClusterLevel c, clj){
		Collection<Transition> nodes = []
        c.nodes.each{
            node ->
            nodes.addAll(node.getTransitionsAsT().findAll(clj))
        }
        return nodes
	}
	
	public double evaluate(ClusterHead tree){
		

		
		int e = tree.children.collect{
		    outEdges(it).size()
        }.sum(0)
        
	
        int n = tree.children.size()
		return e - n + 2
        
	}
	
	public static void main(String[] args){
		new TopLevelCyclomaticMetric().proc(args);
	}

	
}